<?php
namespace Member\Controller;
use Org\Yike\Member;
class DealController extends CommonController
{
    public function _initialize()
    {
        parent::_initialize();
        $this->is_login();
        $this->status = getStatus('deal');
        $this->menu = getLinkMenuFor(array(2, 8, 10, 17, 20, 21, 22, 23, 24), 1);
    }
    public function index (){
        $model = M ('allocation');
        $table = array ('yk_allocation'=>'a');
        $count = $model->table($table)->where('a.user_id='.$this->user['id'])->count();
        $pager = new \Org\Util\Page($count, 15);
        $arr['count'] = $count;

        $model->table ($table);
        $model->field ('c.*,a.name');
        $model->join ('yk_contract c ON a.contract_id=c.deal_id', 'LEFT');
        $model->where (array ('a.user_id'=>$this->user['id']));
        $model->limit($pager->firstRow.','.$pager->listRows);
        $model->order('c.deal_time desc');
        $arr['list'] = $model->select ();
        $arr['page'] = $pager->show();
        $this->assign ('list', $arr);
        $this->display();
    }
    public function bargain (){
        $used_id = I ('get.id', '');
        $used = M ('used')->field ('id,title,encoded,phone,contacts,sell_type')->where ('id='.$used_id)->find ();
        $this->assign ('info', $used);
        $this->display ();
    }
    public function add (){
        $post_data = I ('post.','')['data'];
        $post_data['create_time'] = date ('Y-m-d');
        $data = $this->_insertPic($post_data);
        $data['photo'] = $data['file'];
        M('contract')->create($data);
        $res = M ('contract')->add ($data);
        if ($res){
            $this->success('提交成功', U('deal/index'));
        }else{
            $this->error('提交失败');
        }
    }
    public function edit (){
        if (IS_POST){
            $post_data = I ('post.','')['data'];
            $data = $this->_insertPic($post_data);
            if (empty ($data['file'])) $data['photo'] = $data['file'];
            $res = M ('contract')->where('deal_id='.$data['deal_id'])->save ($data);
            if ($res){
                $this->success('编辑成功', U('deal/index'));
            }else{
                $this->error('提交失败');
            }
        }else{
            $dealid = I ('get.id', '');
            $contract = M ('contract')->where ('deal_id='.$dealid)->find ();
            $this->assign ('info', $contract);
            $this->display ();
        }

    }
    public function del (){
        $id = I ('get.id', '');
        $res = M ('contract')->where ('deal_id='.$id.'status=1')->delete();
        if ($res){
            //删除关联的表
            M ('allocation')->where ('contract_id='.$id)->delete();
            $this->success('删除成功', U('deal/index'));
        }else{
            $this->error('删除失败');
        }
    }
    public function details (){
        $deal_id = I ('get.id', '');
        $model = M ('contract');
        if (IS_AJAX){
            $model->field ('proprietor,p_phone,p_card,custid,customer,c_phone,c_card,remark,status');
            $model->where (array ('deal_id'=>$deal_id));
            $deals = $model->find ();
            if ($deals){
                //记录查看信息
                $msg = '查看合同买卖双方的信息';
                $data = array (
                    'user_id' => $this->user['id'],
                    'source_id' => $deal_id,
                    'uname' => $this->user['username'],
                    'type' => 3,
                    'discuss' => $msg,
                    'add_time' => time()
                );
                M ('follow')->add ($data);
                exit (json_encode (array ('state'=>'ok','data'=>$deals)));
            }else{
                exit (json_encode (array ('state'=>'fail','data'=>'')));
            }
        }
        $model->field ('c.deal_id,c.used_id,c.title,c.sell_type,c.money,c.poundage,c.received,c.deduct,c.diary,c.deal_time');
        $model->table (array ('yk_contract'=>'c'));
        $model->where (array ('deal_id'=>$deal_id));
        $deals = $model->find ();
        $allocs = M ('allocation')->where ('contract_id='.$deal_id)->select ();
        $this->assign ('deal', $deals);
        $this->assign ('allocs', $allocs);
        $this->display ();
    }
    public function retrieve (){
        $type = I ('post.s_type', 2);
        $condition = I ('post.s_cond', '');
        $end = I ('post.s_end', '');

        switch ($type){
            case 1:
                $star_time = $condition.' 00:00:00';
                $end_time = $end.' 00:00:00';
                $map['c.deal_time']  = array('between',array ($star_time,$end_time));
                break;
            case 2:
                $map['c.title'] = array('like','%'.$condition.'%');
                break;
        }
        $map['a.user_id'] = $this->user['id'];

        $model = M ('allocation');
        $table = array ('yk_allocation'=>'a');
        $model->table ($table);
        $model->field ('c.*,a.name');
        $model->join ('yk_contract c ON a.contract_id=c.deal_id', 'LEFT');
        $model->where ($map);
        $count = $model->count ();
        $pager = new \Org\Util\Page($count, 15);
        $arr['count'] = $count;


        $model->table ($table);
        $model->field ('c.*,a.name');
        $model->join ('yk_contract c ON a.contract_id=c.deal_id', 'LEFT');
        $model->where ($map);
        $model->limit($pager->firstRow.','.$pager->listRows);
        $model->order('c.deal_time desc');
        $arr['list'] = $model->select ();
        $arr['page'] = $pager->show();

        $this->assign ('list', $arr);
        $this->display('deal/index');

    }

}